package com.tlias.controller;

import com.tlias.pojo.Dept;

import com.tlias.pojo.Result;
import com.tlias.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    //查找
    @GetMapping("/depts")
    public Result findAllDepts() {
        List<Dept> deptList = deptService.selectDept();
        return Result.success(deptList);
    }

    //id查找
    @GetMapping("/depts/{id}")
    public Result findById(@PathVariable Integer id) {
        Dept byId = deptService.findById(id);
        return Result.success(byId);

    }


    //删除
    @DeleteMapping("/depts/{id}")
    public Result deleteById(@PathVariable Integer id) {
        //日志记录
        log.info("根据id删除部门");
        //调用service层功能
        deptService.deleteById(id);
        //响应
        return Result.success();
    }

    //添加
    @PostMapping("/depts")
    public Result addDept(@RequestBody Dept dept) {
        log.info("根据id添加部门");
        deptService.addDept(dept);
        return Result.success();


    }

    //修改
    @PutMapping("/depts")
    public Result updateById(@RequestBody Dept dept) {
        log.info("修改部门");
        deptService.updateDept(dept);
        return Result.success();
    }


}



